package com.itic.mall.mobile.erp;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.itic.appbase.framework.utils.AESEncryptUtils;
import com.itic.appbase.framework.utils.CalendarHelper;
import com.itic.appbase.framework.utils.DateHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;


/**
 * 用户登入接口类
 * @author 许小宝
 * 2017年07月25日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/erp")
public class MallErpInterface
     extends BaseMobileInterface {
   
    /**
     * APP用户授权登入
     * @return
     */
    @RequestMapping(value="/p")
    @ResponseBody
    public Map<String,Object> p( HttpServletRequest request,HttpServletResponse response,String authCode ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		
    		TokenParamVo tokenParamVo = new TokenParamVo();
    		    
    		tokenParamVo.setTimeStamp(DateHelper.formatDate(CalendarHelper.getDateAfter(new Date(), 10), "yyyy­MM­dd HH:mm:ss"));
    		tokenParamVo.setGrantType("auth_token");
    		tokenParamVo.setAuthParam("5iFYD0V8MfA65WIWBQdqDGssQsfeFUcx");
    		
    		String jsonParam = JSONObject.toJSONString(tokenParamVo);
    				
    		byte[] encrypt = AESEncryptUtils.encrypt(jsonParam,"ONxYDyNaCoyTzsp83JoQ3YYuMPHxk3j7");
    		
    		resultMap.put(DATA, encrypt.toString());
    			
    		errorCode = SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	//log(e);
        	logException("MallLoginInterface", "in", e.toString(),"商城登入接口",e);
    		
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    /**
     * APP用户授权登入
     * @return
     */
    @RequestMapping(value="/sign")
    @ResponseBody
    public Map<String,Object> sign( HttpServletRequest request,HttpServletResponse response,String authCode ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		
    		TokenParamVo tokenParamVo = new TokenParamVo();
    		    
    		tokenParamVo.setTimeStamp(DateHelper.formatDate(CalendarHelper.getDateAfter(new Date(), 10), "yyyy­MM­dd HH:mm:ss"));
    		tokenParamVo.setGrantType("auth_token");
    		tokenParamVo.setAuthParam("5iFYD0V8MfA65WIWBQdqDGssQsfeFUcx");
    		
    		String jsonParam = JSONObject.toJSONString(tokenParamVo);
    				
    		byte[] b = AESEncryptUtils.encrypt(jsonParam,"ONxYDyNaCoyTzsp83JoQ3YYuMPHxk3j7");
    		
    		// 二进制转字符串  
    		StringBuffer sb = new StringBuffer();  
  		    String tmp = "";  
  		    for (int i = 0; i < b.length; i++) {  
	  		    tmp = Integer.toHexString(b[i] & 0XFF);  
	  		    if (tmp.length() == 1){  
	  		        sb.append("0" + tmp);  
	  		    }else{  
	  		        sb.append(tmp);  
	  		    }  
  		      
  		    }  
  		    
  		    //9ad2ea809bd80e56de7a82128ea22476ffdc7f257661ad4f0afe44dc8744686b0fc1208c6efa511077c3f36ea037de02d47e1a40031de548bee7bab4143160af00f131d56d6a70b4ee8a33fc6bbd60e6a697c4929f73d9ed3a3747346c056f7f02fc9e121ea4cd1520b8951f6b3a8dd1    
    		resultMap.put(DATA, sb.toString() );
    		
    		
    			
    		errorCode = SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	//log(e);
        	logException("MallLoginInterface", "in", e.toString(),"商城登入接口",e);
    		
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
}